package groupon.user.bo.service.impl;

import groupon.user.bo.service.ManagerUserService;
import groupon.user.bo.service.ServiceException;
import groupon.user.dao.UserDao;
import groupon.user.model.User;
import groupon.user.model.UserSearch;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service(value ="managerUserService")
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ManagerUserServiceImpl implements ManagerUserService{
	@Resource(name="userDAO")
	private UserDao userDAO;
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void save(User user) throws ServiceException {
		userDAO.save(user);
	}
	
	@Transactional
	public List<User> searchAll() throws ServiceException{
		return userDAO.searchAll();
	}
	
	@Transactional
	public User update(User user) throws ServiceException {
		return userDAO.update(user);
	}
	
	public User findUser(Integer id) throws ServiceException {
		return userDAO.findUser(id);
	}
	
	@Transactional
	public boolean delete(Integer id) throws ServiceException {
		return userDAO.delete(id);
	}

	@Override
	public User findUserByUsernameOrEmail(String username, String email)
			throws ServiceException {
		return userDAO.findUserByUsernameOrEmail(username, email);
	}

	/*
	 * if firstResult = 0 and maxResults = 0,get all user follow usersearchModel
	 * */
	@Override
	public List<User> searchUserInRange(UserSearch userSearch, int firstResult, int maxResults) throws ServiceException {
		return userDAO.searchUserInRange(userSearch, firstResult, maxResults);
	}

	@Override
	public List<User> findInRange(int firstResult, int maxResults)
			throws ServiceException {
		return userDAO.findInRange(firstResult, maxResults);
	}
}
